/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.DefaultListModel;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class Transaction extends javax.swing.JFrame implements Runnable {

    ArrayList cart;
    private int counter;
    Thread trans;
    SuperMarketMainView1 mainscreen;
    String namethread = "trans";
    String result;
    Customer rand_cust;
    Store rand_stor;
    Product rand_prod;
    boolean deli = true;
    DefaultListModel transModel;
    int delay;
   List<Purchase> ListPur ;
   
    public Transaction(SuperMarketMainView1 mainscreen, Customer rand_cust, Store rand_stor, ArrayList cart, int counter) {
        initComponents();
        this.cart = cart;
        this.counter = counter;
        this.mainscreen = mainscreen;
        this.rand_cust = rand_cust;
        this.rand_stor = rand_stor;
        this.transModel = new DefaultListModel();
        ListPur = mainscreen.db.readPurchases();
        jList1.removeAll();//diagrafo ta panta pao ti lista
        if (trans == null) {//αν δεν υπάρχει το νημα
            trans = new Thread(this);//δημιουργία νήματος
            trans.start();//αρχικη

        }
    }

    public void run() {
        jList1.removeAll();//diagrafo ta panta pao ti lista
        Random rand = new Random();//για τυχαιο αριθμό
        int rand_cvv = rand.nextInt(999)+100;//τυχαιος αριθμός cvv
        System.out.println("rand_cvv: "+rand_cvv);
        int cvv = mainscreen.apk.getRecord(rand_cvv);
        
     
        

        
          
            while (true) {
                 jList1.removeAll();//diagrafo ta panta pao ti lista
                 
                try {
                    if (counter > 2) {
            
            result = "NOK";
            transModel.addElement(result);
            jList1.setModel(transModel);
           
            mainscreen.psimular.purchas.stop();
          //  trans.stop();
            break;
            
         
                }
                   
                     System.out.println("retries: "+counter);
                    if (cvv > 0) {//να υπάρχει η κάρτα
                        result = "OK";
                      
                        Iterator itr = cart.iterator();//διατρέχω το καλάθι
                        while (itr.hasNext()) {
                            rand_prod = (Product) itr.next();
                            //νέα αγορα
                            mainscreen.db.makePurchase(rand_cust, rand_stor, rand_prod, rand_cvv, ListPur, deli);
                            transModel.addElement(result);
                        }
                        
                        jList1.setModel(transModel);
                        break;

                    } else {
                       
                        int delay = rand.nextInt(10) + 10;//τυχαιος αριθμός 1-10 sec
                        this.dispose();
                        Thread.sleep(delay * 1000);
                        
                    }


                } catch (InterruptedException e) {
                     System.err.println("interrupted2");
                }
                trans.stop();
            }
        
        


    }

    public String getNamethread() {
        return namethread;
    }

    public String getResult() {
        return result;
    }

    /**
     * Creates new form Transaction
     */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(Transaction.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(158, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(58, 58, 58)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:OK
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
